package com.ruoyi.web.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.web.domain.UserFriend;
import com.ruoyi.web.mapper.UserFriendMapper;
import com.ruoyi.web.service.FileService;
import com.ruoyi.web.service.IUserFriendService;
import com.ruoyi.web.utils.UserMsgUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * friendService业务层处理
 *
 * @author ruoyi
 * @date 2024-04-24
 */
@Service
public class UserFriendServiceImpl implements IUserFriendService {
    @Autowired
    private UserFriendMapper userFriendMapper;
    @Autowired
    private FileService fileService;

    /**
     * 查询当前用户的所有friend信息
     *
     * @return userList
     */
    @Override
    public List<SysUser> selectFriendsByUserId() {
        if (UserMsgUtil.getUserId() != null) {
            // 当前用户存在
            // 查找当前用户的所有好友信息
            List<SysUser> friends = userFriendMapper.selectFriendsByUserId(UserMsgUtil.getUserId());
            // 获取好友的头像oss链接
            friends = friends.stream().peek(item -> {
                if (item.getAvatar() != null) {
                    item.setAvatar(UserMsgUtil.subAvatar(item.getAvatar()));
                }
            }).collect(Collectors.toList());

            return friends;
        }
        // 当前用户不存在
        return null;
    }

    /**
     * 新增friend
     *
     * @param userFriend friend
     * @return 结果
     */
    @Override
    public int insertUserFriend(UserFriend userFriend) {
        return userFriendMapper.insertUserFriend(userFriend);
    }

    /**
     * 修改friend
     *
     * @param userFriend friend
     * @return 结果
     */
    @Override
    public int updateUserFriend(UserFriend userFriend) {
        return userFriendMapper.updateUserFriend(userFriend);
    }

    /**
     * 批量删除friend
     *
     * @param ids 需要删除的friend主键
     * @return 结果
     */
    @Override
    public int deleteUserFriendByIds(Long[] ids) {
        return userFriendMapper.deleteUserFriendByIds(ids);
    }

    /**
     * 删除friend信息
     *
     * @param id friend主键
     * @return 结果
     */
    @Override
    public int deleteUserFriendById(Long id) {
        return userFriendMapper.deleteUserFriendById(id);
    }
}
